﻿package  {
	
	import com.Enemy;
	import com.Key;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.utils.getTimer;

	public class Dodge extends Sprite {
		
		// player.height = 45;
		public var player:MovieClip;
		public var frame:MovieClip;
		
		private var original_point:Point;
		private var enemys:Array;
		private var keysDown:Object;
		private var keycooltime:int;
		private var lasttimekey:int;
		private var lasttimeAddEnemy:int;
		private var room:Rectangle;
		private var yrg:Sprite;
		
		public function Dodge() 
		{
			addEventListener(Event.ADDED_TO_STAGE, onStage);
		}
		
		private function onStage(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, onStage);
			original_point = new Point(player.x, player.y);
			init();
		}
		
		private function init():void 
		{
			room = new Rectangle(frame.x - 200, frame.y, frame.width + 400, frame.height);
						
			var f:Sprite = new Sprite();
			f.graphics.lineStyle(1, 0x999999);
			f.graphics.drawRect(room.x, room.y, room.width, room.height);
			f.graphics.endFill();
			addChild(f);
			
			if (yrg) {
				removeChild(yrg);
				yrg = null;
			}
			
			if (enemys) {
				while (enemys.length > 0) {
					removeChild(enemys.shift());
				}				
			}
			
			enemys = [];
			
			if (original_point) {
				player.x = original_point.x;
				player.y = original_point.y;
			}
			
			lasttimeAddEnemy = lasttimekey = 0;
			
			keycooltime = 100;
			keysDown = { };
			
			createEnemy();
			
			//frame.visible = false;
			
			stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPress);
			stage.addEventListener(KeyboardEvent.KEY_UP, onKeyRelease);
			addEventListener(Event.ENTER_FRAME, update);
			
		}
		
		private function addEnemy():void
		{
			var t:int = getTimer();
			if (t - lasttimeAddEnemy >= 3000) 
			{
				lasttimeAddEnemy = t;
				createEnemy();			
			}
		}
		
		private function createEnemy():void
		{
			var e:Enemy = new Enemy();
			e.speed = 2 + Math.random() * 2;
			e.x = room.left;
			e.y = frame.y + Math.random() * frame.height;
			if (Math.random() > 0.5) {
				e.scaleX *= -1;
				e.speed *= -1;
				e.x = room.right;
			}
			
			addChild(e);
			enemys.push(e);
		}
		
		private function onClick(e:MouseEvent):void
		{
			e.currentTarget.removeEventListener(e.type, arguments.callee);
			init();
		}
		
		private function onKeyPress(e:KeyboardEvent):void 
		{
			keysDown[e.keyCode] = true;
		}
		
		private function onKeyRelease(e:KeyboardEvent):void 
		{
			delete keysDown[e.keyCode];
		}
		
		private function isDown(keycode:uint):Boolean
		{
			return keysDown[keycode];
		}
		
		private function update(e:Event):void 
		{			
			addEnemy();
			keyHandle();
			moveEnemy();
			
			if (getTimer() > 10000 && Math.random() > 0.95) {
				createBOSS();
			}
		}
		
		private function createBOSS():void
		{
			var e:Enemy = new Enemy();
			e.speed = 25;
			e.x = room.left;
			e.y = frame.y + Math.random() * frame.height;
			e.scaleX = e.scaleY = 1.3;
			if (Math.random() > 0.5) {
				e.scaleX *= -1.3;
				e.speed *= -1;
				e.x = room.right;
			}
			
			addChild(e);
			enemys.push(e);
		}
		
		private function keyHandle():void 
		{
			var t:int = getTimer();
			if (isDown(Key.UP) && t - lasttimekey >= keycooltime)
			{
				if (player.y > room.top) {
					player.y -= 45;
					lasttimekey = t;
				}
				
			} else if (isDown(Key.DOWN) && t - lasttimekey >= keycooltime) {
				
				if (player.y + player.height + 5 < room.bottom) {					
					player.y += 45;
					lasttimekey = t;
				}
			}
		}
		
		private function moveEnemy():void 
		{
			for each(var e:Enemy in enemys) 
			{				
				if (player.hitTestObject(e)) {
					gameover();
				} else {					
					e.move();
				}
				
				if (e.x < room.left || e.x > room.right) {
					enemys.splice(enemys.indexOf(e), 1);
					e.Destroy();
				}
			}
		}
		
		private function gameover():void
		{
			trace("GAMEOVER");
			stage.removeEventListener(KeyboardEvent.KEY_DOWN, onKeyPress);
			stage.removeEventListener(KeyboardEvent.KEY_UP, onKeyRelease);
			stage.addEventListener(KeyboardEvent.KEY_UP, onKeyRelease);
			removeEventListener(Event.ENTER_FRAME, update);
			
			yrg = new Gay();
			addChild(yrg);
			yrg.x = (stage.stageWidth - yrg.x) * 0.5;
			yrg.y = (stage.stageHeight - yrg.y) * 0.5;
			yrg.addEventListener(MouseEvent.CLICK, onClick);
		}
	}
}
